import throttle from "https://cdn.jsdelivr.net/npm/lodash-es/throttle.js";

export async function HitAction(Hand, action, Music = () => false) {
    const event = throttle(async () => {
        if (app.userLock === false) {
            app.userLock = true;
            const isBump = window.B.hitTestRectangle(
                Hand,
                app.stage.Flower.HitObject
            );

            if (isBump) {
                const { x, y, width, height } = app.stage.Flower;
                await Hand.anime({
                    x: x - 60 * px,
                    y: y,
                    duration: 100,
                });
                await Promise.all([Hand.Loop(2), Music && Music()]);
                Promise.resolve(action()).then(() => {
                    app.userLock = false;
                });
            } else {
                return Promise.resolve(
                    Hand.anime(Hand.dragMessage.lockPlace)
                ).then(() => {
                    app.userLock = false;
                });
            }
        }
    }, 100);
    Hand.on("pointerup", event);
}
